package com.ruoyi.web.controller.app;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.app.domain.vo.AppSalesLevelVo;
import com.ruoyi.app.domain.bo.AppSalesLevelBo;
import com.ruoyi.app.service.IAppSalesLevelService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 代理等级
 *
 * @author ruoyi
 * @date 2025-07-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/salesLevel")
public class AppSalesLevelController extends BaseController {

    private final IAppSalesLevelService iAppSalesLevelService;

    /**
     * 查询代理等级列表
     */
    @SaCheckPermission("system:salesLevel:list")
    @GetMapping("/list")
    public TableDataInfo<AppSalesLevelVo> list(AppSalesLevelBo bo, PageQuery pageQuery) {
        pageQuery.setIsAsc("asc");
        pageQuery.setOrderByColumn("id");
        return iAppSalesLevelService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出代理等级列表
     */
    @SaCheckPermission("system:salesLevel:export")
    @Log(title = "代理等级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppSalesLevelBo bo, HttpServletResponse response) {
        List<AppSalesLevelVo> list = iAppSalesLevelService.queryList(bo);
        ExcelUtil.exportExcel(list, "代理等级", AppSalesLevelVo.class, response);
    }

    /**
     * 获取代理等级详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:salesLevel:query")
    @GetMapping("/{id}")
    public R<AppSalesLevelVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iAppSalesLevelService.queryById(id));
    }

    /**
     * 新增代理等级
     */
    @SaCheckPermission("system:salesLevel:add")
    @Log(title = "代理等级", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppSalesLevelBo bo) {
        return toAjax(iAppSalesLevelService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改代理等级
     */
    @SaCheckPermission("system:salesLevel:edit")
    @Log(title = "代理等级", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppSalesLevelBo bo) {
        return toAjax(iAppSalesLevelService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除代理等级
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:salesLevel:remove")
    @Log(title = "代理等级", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iAppSalesLevelService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
